<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @add="fun" :list="list"></TodoHeader>
    <TodoMain :list="showlist" @del="fn"></TodoMain>
    <TodoFooter @change="exhibit" :list="list" @del="dellist"></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import "./styles/base.css"
import "./styles/index.css"
    
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";


export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      isShow:'all',
      // list: [
      //   { id: 100, name: "吃饭", isDone: true },
      //   { id: 101, name: "睡觉", isDone: false },
      //   { id: 102, name: "打豆豆", isDone: true },
      // ],
      list:JSON.parse(localStorage.getItem('newlist'))||[]
    };
  },
  methods: {
    fn(ind){
     this.list.splice(ind,1)
    },
    fun(val){
      this.list.push({
        id:this.list.length+100,
        name:val,
        isDone:false,
      })
    },
    exhibit(val){
       this.isShow=val
    },
    dellist(){
     this.list=this.list.filter(item=>item.isDone===false)
    }
  },
  computed: {
    showlist(){
      if(this.isShow==='yes'){
       return  this.list.filter(item=>item.isDone===true)
       }else if (this.isShow==='no'){
      return this.list.filter(item=>item.isDone===false)
       }else {
       return this.list
       }
    }
  },
  watch:{
    list:{
      deep:true,
      handler(){
        localStorage.setItem('newlist', JSON.stringify(this.list))
      }
    }
  }

};
</script>